<?php
global $trdb,$PHP_SELF,$db_query;
	
$feed_name = $db_query->query_vars['feed'];

if($feed_name == "comments") {
	$rss = new UniversalFeedCreator(); 
	$rss->title = get_option('sitetitle'); 
	$rss->description = "评论feed"; 
	$rss->link = get_option('siteurl'); 
	$rss->syndicationURL = get_option('siteurl').$PHP_SELF;

	$request = "SELECT a.comment_post_id, a.comment_user_id, a.comment_content, UNIX_TIMESTAMP(a.comment_date) AS comment_date, b.user_login FROM $trdb->comments a, $trdb->users b";
	$request .= " WHERE a.comment_approved = '1'";
	$request .= " AND a.comment_user_id = b.ID";
	$request .= " ORDER BY a.comment_date DESC LIMIT 15";
	$posts = $trdb->get_results($request);
	$post_num = count($posts);

	for ($i=0;$i<$post_num;$i++) {
		$item = new FeedItem();
		$post = $posts[$i];
		$item->title = 'comment by '.$post->user_login;
		$item->link = get_option('siteurl')."/?q=".$post->comment_post_id;
		$item->description = $post->comment_content;
		$item->date = (int)$post->comment_date;
		$item->author = $post->user_login;
		
		$rss->addItem($item); 
	}
}
else {
	$rss = new UniversalFeedCreator(); 
	$rss->title = get_option('sitetitle'); 
	$rss->description = get_option('sitetitle'); 
	$rss->link = get_option('siteurl'); 
	$rss->syndicationURL = get_option('siteurl').$PHP_SELF;

	$request = "SELECT a.ID, a.post_author, a.post_title, a.post_content, UNIX_TIMESTAMP(a.post_favor_date) AS post_favor_date, b.user_login FROM $trdb->posts a, $trdb->users b";
	$request .= " WHERE a.post_status = 'publish'";
	$request .= " AND a.post_author = b.ID";
	$request .= " ORDER BY a.post_date DESC";
	$request .= " LIMIT 20";
	$posts = $trdb->get_results($request);
	$post_num = count($posts);

	for ($i=0;$i<$post_num;$i++) {
		$item = new FeedItem();
		$post = $posts[$i];
		$item->title = $post->post_title;
		$item->link = get_option('siteurl')."/?q=".$post->ID;
		$item->description = $post->post_content;
		$item->date = (int)$post->post_favor_date;
		$item->author = $post->user_login;
		
		$rss->addItem($item); 
	}
}

$rss->saveFeed("RSS2.0");

?>